<?php
namespace Admin\Controller;
use Think\Controller;
header('Content-Type: text/html; charset=utf-8');
class GuanController extends Controller{
	/*****************************站内关注 及私信发送 私信查看********************************/
	/***************************私信发送为ID 一次只可发送一条私信******************************/
	/********关注ID为1 session存储 实际运营需要修改session存储的ID 用户登录时存储*********/
	/*关注查看的默认ID 为1  因为是ID存储 所以只能存一个ID
	 */
	//后台关注首页
	function guan(){
		$_SESSION['user_id'] = '4';
		$user = M('user');
		$show = $user->select();
		$this->assign('show',$show);
		$this->display();
	}
	//后台AJAX关注返回
	function guanzhu(){
		$id  = I('get.user_id');
		$guan = M('guan');
		$data['guan_user_id'] = $_SESSION['user_id'];
		$data['guan_user_pid'] = $id;
		if($guan->add($data)){
			echo json_encode(1);
		}else{
			echo json_encode(2);
		}
	}
	//关注查看	
	function guan2(){
		$name = M('user as a')
				->field('user_name,user_id')
				->join('dr_guan as b on b.guan_user_pid=a.user_id')
				->where('a.user_id='.$_SESSION['user_id'])
				->select();
		$this->assign('name',$name);
		$this->display();
	}
	//私信发送
	function tell(){
		if(IS_POST){
			$data['tell_user_pid'] = I('post.user_pid');
			$data['tell_text']  = I('post.tell_text');
			$data['tell_user_id']   = $_SESSION['user_id'];
			$tell = M('tell');
			if($tell->add($data)){
				$this->success('成功');
			}else{
				$this->error('失败');
			}
		}else{
			$id = I('get.user_id');
			$show = M('user')->field('user_name,user_id')->where('user_id='.$id)->find();
			$this->assign('show',$show);
			$this->display();
		}
	}
	//私信(接受)  查看
	function tell2(){
		//这里显示的为当前ID发送的私信
		// $tell = M('user as a')
		// 		->field('user_name,tell_text')
		// 		->join('dr_tell as b on b.tell_user_id=a.user_id')
		// 		->where('b.tell_user_id='.$_SESSION['user_id'])
		// 		->select();
		// var_dump($tell);

		
		
		
		// 发送人一号  接收人三号 
		// 以下是不显示接收人的SQL语句  当前ID接受的私信
		$tell = M('user as a')->field('user_name,tell_text,tell_user_id')
				->join('dr_tell as b on b.tell_user_id=a.user_id')
				->where('b.tell_user_pid='.$_SESSION['user_id'])
				->select();

		$this->assign('tell',$tell);
		$this->display();
		// ***站内私信可以使用redis来实现功能  PHP开启redis扩展 开启redis redis-server redis.conf 
	      // 引入redis  $a = new redis();   设置redis  $a->redis->set();   
		}

	//私信发送  存储在redis  存储格式为 list 接收人暂定为user_name接受
		function redis(){
			if(IS_POST){
				$_SESSION['user_name'] = '发送人1';
					$redis = new \Think\Cache\Driver\SinglePullMessage();
					$sender = $_SESSION['user_name']; //发送人
					$to = "十里";  		//接收人
					$message = I('post.tell_text'); //内容
					$time = time();  //时间
					$arr=array('sender'=>$sender,'message'=>$message,'time'=>$time);
					$ac =  $redis->sendSingle($to,$arr);
					var_dump($ac);
			}else{
					$id = I('get.user_id');
					$show = M('user')->field('user_name,user_id')->where('user_id='.$id)->find();
					$this->assign('show',$show);
					$this->display();
			}
		}
		//私信新消息查看 
		function redis_get(){
			$redis = new \Think\Cache\Driver\SinglePullMessage();
			$arr = $redis->getNewMessage('十里');
				var_dump($arr);			
			echo $redis->dealArr($arr);
		}
		//私信查看所有消息 
		function redis_gett(){
			$redis = new \Think\Cache\Driver\SinglePullMessage();
			$arr = $redis->getPreMessage('十里');
				print_r($arr);
				//dealArr 后台处理消息函数 可直接发送到前台	
				//$arr['sender']		
			echo $redis->dealArr($arr);
		}
}	
	